<?php

class QuizController extends Controller
{
	public $layout='menu';
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
				$arrViewaction=array('view','index','admin');
				$arrUpdateaction=array('create','update','delete');
				//return Yii::app()->GetAccessRule->get($arrViewaction,$arrUpdateaction);
        return array();
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		if (!Yii::app()->user->isGuest)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}
	else
	 $this->redirect(Yii::app()->createUrl('/admin/login'));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		if (!Yii::app()->user->isGuest)
	{
		$model=new Quiz;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Quiz']))
		{
			$fn=Quiz::model()->find('title=? and user_id=?',array($_POST['Quiz']['title'],Yii::app()->user->id));
		if($fn)
		{
			Yii::app()->user->setFlash('success', "quiz already present. please enter another name");
		}
		else
		{
			$model->attributes=$_POST['Quiz'];
			$model->user_id=Yii::app()->user->id;
			
			if($model->save())
			{
				Yii::app()->user->setFlash('success', "Quiz created successfully");
				$this->redirect(Yii::app()->createUrl('/admin/quiz/admin/id/'.$model->category_id));
			}
		}
		}
		$this->render('create',array(
			'model'=>$model,
		));
	}
	else
	 $this->redirect(Yii::app()->createUrl('/admin/login'));
	}
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
			if (!Yii::app()->user->isGuest)
	{
		$model=$this->loadModel($id);
		if(isset($_POST['Quiz']))
		{
			$model->attributes=$_POST['Quiz'];
			if($model->save())
			Yii::app()->user->setFlash('success', "Quiz updated successfully");
			$this->redirect(Yii::app()->createUrl('/admin/quiz/admin'));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}
	else
	 $this->redirect(Yii::app()->createUrl('/admin/login'));
	}
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if (!Yii::app()->user->isGuest)
	{
		$id=$_GET['id'];
		$quiz=Questions::model()->findAll('quiz_id=?',array($id));
		$obj=Quiz::model()->find('id=?',array($id));
		if(!empty($obj) && $obj->delete())
		{
		if(!empty($quiz))
		{
			foreach ($quiz as $objquestion)
			{
				if($objquestion->delete())
				{
					$options=Options::model()->findAll('question_id=?',array($objquestion->id));
						if(!empty($options))
						{
							foreach ($options as $option)
							$option->delete();
						}			
				}
			}		
		
		Yii::app()->user->setFlash('success', "Quiz deleted successfully");
		$this->redirect(Yii::app()->createUrl('admin/quiz/admin/id/'.$id));
		
		}
	   }
	}
	else
	 $this->redirect(Yii::app()->createUrl('/admin/login'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
			if (!Yii::app()->user->isGuest)
	{
		$dataProvider=new CActiveDataProvider('Quiz');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
	if (!Yii::app()->user->isGuest)
	{
		$model=new Quiz('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Quiz']))
			$model->attributes=$_GET['Quiz'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	else
	 $this->redirect(Yii::app()->createUrl('/admin/login'));
	}
	
	public function loadModel($id)
	{
			if (!Yii::app()->user->isGuest)
	{
		$model=Quiz::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	else
	 $this->redirect(Yii::app()->createUrl('/admin/login'));
	}
	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='quiz-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
